package com.book.service.impl;

import com.book.entity.Menu;
import com.book.entity.RoleMenu;
import com.book.mapper.MenuMapper;
import com.book.mapper.RoleMenuMapper;
import com.book.service.MenuService;
import com.book.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author SuperYu  2024/6/26
 */
@Service
public class MenuServiceImpl implements MenuService {
    MenuMapper menuMapper;

    @Autowired
    public void setMenuMapper(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }

    RoleMenuMapper roleMenuMapper;

    @Autowired
    public void setRoleMenuMapper(RoleMenuMapper roleMenuMapper) {
        this.roleMenuMapper = roleMenuMapper;
    }

    RedisTemplate<String, Object> redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Autowired
    StringRedisTemplate stringRedisTemplate;


    @Override
    @Cacheable(value = "menu", key = "#root.methodName+#root.args[0]")
    public List<MenuVo> findMenuByUserName(String name) {
        return menuMapper.selectMenuByUserName(name);
    }

    // @Override
    // public List<MenuVo> findMenuByUserName(String name) {
    //     // 1： 先看看缓存里面有没有数据
    //     Object o = redisTemplate.opsForValue().get("MenuList:" + name);
    //     if (o != null) {
    //         // 1-1：有数据，就直接返回数据，不用再查数据库
    //         return (List<MenuVo>) o;

    @Override
    public Set<Long> findPermissionByRoleId(int id) {
        Example example = new Example(RoleMenu.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("roleId", id);

        List<RoleMenu> list = roleMenuMapper.selectByExample(example);
        HashSet<Long> set = new HashSet<Long>();
        for (RoleMenu roleMenu : list) {
            set.add(roleMenu.getMenuId());
        }
        return set;
    }
    //     } else {
    //         // 1-2：没数据，就查询数据库，再写入缓存再返回数据
    //         List<MenuVo> list = menuMapper.selectMenuByUserName(name);

    @Override
    public void findPermissionByUserName(String name) {
        // 1: 先从redis里面查
        Set<String> members = stringRedisTemplate.opsForSet().members("permission:"+name);

        if (members.isEmpty()) {
            // 2：没有就从数据库查
            List<String> list = menuMapper.selectPermissionByUserName(name);
            String[] ss = new String[list.size()];

            for (int i = 0; i < ss.length; i++) {
                ss[i] = list.get(i);
            }
            stringRedisTemplate.opsForSet().add("permission:"+name, ss);
            stringRedisTemplate.expire("permission:"+name,15, TimeUnit.MINUTES);
        }
    }


    @CacheEvict(value = "menu", allEntries = true)
    public int addPermission(Menu menu) {

        return 0;
    }


    @Override
    @Cacheable(value = "menu", key = "#root.methodName")
    public List<MenuVo> findAllPermission() {
        return menuMapper.selectAllPermission();
    }
}
